package cn.org.guhao.practice.go.util;

/**
 * 数组工具类
 * @author guhao
 * @since 2020-03-13
 */
public class ArrayUtils {

  /**
   * 二分法查找
   * @param array
   * @param value
   * @return
   */
  public static int binarySearch(int[] array,int value){
    int middleIndex = -1;
    int low = 0;
    int hight = array.length - 1;
    while (low <= hight){
      middleIndex = (low+hight)/2;
      int mVal = array[middleIndex];
      if(mVal == value){
        return middleIndex;
      }
      if(mVal > value){
        hight = middleIndex - 1;
      } else {
        low = middleIndex + 1;
      }
    }
    return middleIndex;
  }

}
